package com.example.sys.dingtalk.service;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse;
import com.example.core.utils.StringUtils;
import com.example.sys.dingtalk.constant.DingTalkConstant;
import com.example.sys.org.constant.OrgConstant;
import com.example.sys.org.service.SysElementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/***
 *
 * 钉钉部门服务类
 */
@Service
public class DingTalkDeptServiceImpl implements DingTalkDeptService {

    public static final Long ROOT = 1L;

    public static final String DEPT_ID_LIST = "dept_id_list";

    @Resource
    private DingTalkTokenService dingTalkTokenService;

    @Resource
    private SysElementService sysElementService;

    /***
     *
     * 获取id的直接下级的id
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List<Long> getDeptIdListByPid(Long id) throws Exception {
        List<Long> list = new ArrayList<>();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
        OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
        req.setDeptId(id);
        OapiV2DepartmentListsubidResponse response = client.execute(req, dingTalkTokenService.getToken());
        if (response != null && StringUtils.isNotEmpty(response.getBody())) {
            JSONObject body = JSONObject.parseObject(response.getBody());
            if (body != null && body.containsKey(DingTalkConstant.RESULT) && body.getString(DingTalkConstant.ERRMSG).equalsIgnoreCase(DingTalkConstant.OK)) {
                JSONObject result = body.getJSONObject(DingTalkConstant.RESULT);
                if (result != null && result.containsKey(DEPT_ID_LIST)) {
                    list = result.getJSONArray(DEPT_ID_LIST).toJavaList(Long.class);
                }
            }
        }
        return list;
    }

    /***
     *
     * 获取所有的部门的id
     * @return
     * @throws Exception
     */
    @Override
    public List<Long> getDeptIdList() throws Exception {
        ArrayList<Long> list = new ArrayList<Long>(Arrays.asList(ROOT));
        return getDeptIdList(ROOT, list);
    }

    /***
     * 循环获取所有的部门
     * @param pid
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public List<Long> getDeptIdList(Long pid, List<Long> list) throws Exception {
        List<Long> child = getDeptIdListByPid(pid);
        if (!child.isEmpty()) {
            list.addAll(child);
        }
        for (Long id : child) {
            if (!existDeptChild(id)) {
                getDeptIdList(id, list);
            }
        }
        return list;
    }

    /***
     *
     * 判断钉钉的部门是否存在,返回true表示没有子部门信息。返回false表示存在子部门信息。
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public boolean existDeptChild(Long id) throws Exception {
        return getDeptIdListByPid(id).isEmpty();
    }


    /****
     *
     * 判断那些部门目前不存在系统中
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public List<Long> checkExistDeptId(List<Long> list) throws Exception {
        return sysElementService.checkOrgElementId(OrgConstant.DEPT, list);
    }

    @Override
    public void getDeptInfoById(Long id) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
        OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
        req.setDeptId(id);
        OapiV2DepartmentGetResponse rsp = client.execute(req, dingTalkTokenService.getToken());
        System.out.println(rsp.getBody());
    }
}
